﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_TaskAdd : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DataBaseHelper.BindDrp(Drp_Contents, EnumData.DataBaseEunm.TaskContent);

                txt_Task_Implement.OnClientTriggerClick = Window1.GetSaveStateReference(hid_Task_ImplementID.ClientID, txt_Task_Implement.ClientID)
                  + Window1.GetShowReference("/PublicModel/SelectUsers.aspx?single=1", "选择执行人");


                txt_Task_Organiser.OnClientTriggerClick = Window1.GetSaveStateReference(hid_Task_OrganiserID.ClientID, txt_Task_Organiser.ClientID)
                 + Window1.GetShowReference("/PublicModel/SelectUsers.aspx?single=1", "选择发起人");

                btnClose.OnClientClick = ActiveWindow.GetConfirmHideReference();
                LoadData();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["pid"]))
            {
                Proj_Main con = TbsDbSession.Default.From<Proj_Main>().Where(Proj_Main._.ID == int.Parse(Request["pid"])).ToFirst();
                txt_Proj_No.Text = con.PRJNUB;
                txt_ProjName.Text = con.DESTON;
            }
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Proj_Task PTask = TbsDbSession.Default.From<Proj_Task>().Where(Proj_Task._.ID == int.Parse(Request["id"])).ToFirst();
                if (PTask != null)
                {
                    txt_Task_Organiser.Text = PTask.Task_Organiser;
                    txt_Task_Implement.Text = PTask.Task_Implement;
                    hid_Task_OrganiserID.Text = PTask.Task_OrganiserID.Value.ToString();
                    hid_Task_ImplementID.Text = PTask.Task_ImplementID.Value.ToString();
                   // txt_Reality_FinishTime.Text = PTask.Reality_FinishTime.Value.ToString("yyyy-MM-dd");
                    txt_Plan_FinishTime.Text = PTask.Plan_FinishTime.Value.ToString("yyyy-MM-dd");
                    txt_Proj_No.Text = PTask.Proj_NO;
                    Drp_Contents.SelectedValue = PTask.Contents;
                    Drp_Task_State.SelectedValue = PTask.Task_State.Value.ToString();
                    txt_ProjName.Text = PTask.Proj_Name;
                    txt_Memo.Text = PTask.Memo;
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Proj_Task PTask = new Proj_Task();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                PTask.ID = int.Parse(Request["id"]);
                PTask.Attach();
                PTask.Contents = Drp_Contents.SelectedValue; //txt_Contents.Text;
                PTask.Plan_FinishTime = Convert.ToDateTime(txt_Plan_FinishTime.Text);

                if (Drp_Task_State.SelectedValue == "2")
                    PTask.Reality_FinishTime = DateTime.Now;
                PTask.Task_Implement = txt_Task_Implement.Text;
                PTask.Task_ImplementID = int.Parse(hid_Task_ImplementID.Text.Replace(",",""));
                PTask.Task_Organiser = txt_Task_Organiser.Text;
                PTask.Task_OrganiserID = int.Parse(hid_Task_OrganiserID.Text.Replace(",", ""));
                PTask.Task_State = int.Parse(Drp_Task_State.SelectedValue);
                PTask.Proj_Name = txt_ProjName.Text;
                PTask.Memo =txt_Memo.Text;
                //PMain.COTYPE = txt_COTYPE.Text;
                //PMain.CONTAT = txt_CONTAT.Text;
                //PMain.ADDRES = txt_ADDRES.Text;
                //PMain.DESTON = txt_DESTON.Text;
                //PMain.STATUS = Drp_STATUS.SelectedValue;
                //PMain.Team_Mem = txt_Team_Mem.Text;
                //PMain.ReMark = txt_ReMark.Text;
                returnvalue = TbsDbSession.Default.Update<Proj_Task>(PTask);
            }
            else
            {
                PTask.DeAttach();
                PTask.Contents = Drp_Contents.SelectedValue;// txt_Contents.Text;
                PTask.Plan_FinishTime = Convert.ToDateTime(txt_Plan_FinishTime.Text);
                if (Drp_Task_State.SelectedValue == "2")
                    PTask.Reality_FinishTime = DateTime.Now;
                PTask.Task_Implement = txt_Task_Implement.Text;
                PTask.Task_ImplementID = int.Parse(hid_Task_ImplementID.Text.Replace(",", ""));
                PTask.Task_Organiser = txt_Task_Organiser.Text;
                PTask.Task_OrganiserID = int.Parse(hid_Task_OrganiserID.Text.Replace(",", ""));
                PTask.Proj_Type = 0;// int.Parse(Request["mtype"].ToString());
                PTask.Proj_NO = txt_Proj_No.Text;
                PTask.Proj_Name = txt_ProjName.Text;
                PTask.Task_State = int.Parse(Drp_Task_State.SelectedValue);
                PTask.Adder = CookInfo.EAdminTrueName();
                PTask.AddID = CookInfo.EAdminid();
                PTask.AddTime = DateTime.Now;
                PTask.Memo = txt_Memo.Text;
                //PMain.PRJNUB = DFieldSN.SN(EnumData.DataSNEunm.项目编号);
                //PMain.COTYPE = txt_COTYPE.Text;
                //PMain.CONTAT = txt_CONTAT.Text;
                //PMain.ADDRES = txt_ADDRES.Text;
                //PMain.DESTON = txt_DESTON.Text;
                //PMain.Adder = CookInfo.EAdminTrueName();
                //PMain.AddID = CookInfo.EAdminid();
                //PMain.AddTime = DateTime.Now;
                //PMain.STATUS = Drp_STATUS.SelectedValue;
                //PMain.Team_Mem = txt_Team_Mem.Text;
                //PMain.ReMark = txt_ReMark.Text;
                returnvalue = TbsDbSession.Default.Insert<Proj_Task>(PTask);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！","提示",ActiveWindow.GetHideRefreshReference());
            }
        }

    }
}